package resolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"errors"
	"fmt"
	"strconv"

	"mall/service/forum/rpc/forumclient"
	"mall/service/wpgql/api/internal/graph/auth"
	"mall/service/wpgql/api/internal/graph/generated"
	"mall/service/wpgql/api/internal/graph/helper"
	"mall/service/wpgql/api/internal/graph/model"
	"mall/service/wpgql/api/internal/graph/util"
)

const forumUserSingleError = "could not get user"
const forumUserListError = "could not list users"

func (r *forumUserResolver) Friendships(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumFriendshipConnectionSorter, filter *model.ForumFriendshipConnectionFilter) (*model.ForumFriendshipConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *forumUserResolver) Topics(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumTopicConnectionSorter, filter *model.ForumTopicConnectionFilter) (*model.ForumTopicConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *forumUserResolver) Posts(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumPostConnectionSorter, filter *model.ForumPostConnectionFilter) (*model.ForumPostConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *forumUserResolver) Comments(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumCommentConnectionSorter, filter *model.ForumCommentConnectionFilter) (*model.ForumCommentConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *forumUserResolver) Likes(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumReactionConnectionSorter, filter *model.ForumReactionConnectionFilter) (*model.ForumReactionConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *forumUserResolver) CommentLikes(ctx context.Context, obj *model.ForumUser, first *int, last *int, after *string, before *string, sorter []*model.ForumCommentReactionConnectionSorter, filter *model.ForumCommentReactionConnectionFilter) (*model.ForumCommentReactionConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) CreateForumUser(ctx context.Context, input model.CreateForumUserInput) (*model.ForumUserPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) UpdateForumUser(ctx context.Context, id string, input model.UpdateForumUserInput) (*model.ForumUserPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) DeleteForumUser(ctx context.Context, id string) (*model.DeleteForumUserPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) CreateForumFriendship(ctx context.Context, input model.CreateForumFriendshipInput) (*model.ForumFriendshipPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) UpdateForumFriendship(ctx context.Context, id string, input model.UpdateForumFriendshipInput) (*model.ForumFriendshipPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) DeleteForumFriendship(ctx context.Context, id string) (*model.DeleteForumFriendshipPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) DeleteForumFriendships(ctx context.Context, filter *model.ForumFriendshipConnectionFilter) (*model.DeleteForumFriendshipsPayload, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) ForumUser(ctx context.Context, id string) (*model.ForumUser, error) { ////
	userId := auth.ForumUserIDFromContext(ctx)

	req := &forumclient.UserDetailRequest{
		VisitorId: int64(userId),
	}

	req.Id = helper.ForumUserID(id)

	preload, eagerLoad := helper.GetForumUserPreloads(ctx) 
	if preload != "" {
		req.Preload = preload
	}
	if eagerLoad != "" {
		req.EagerLoad = eagerLoad
	}

	res, err := r.svcCtx.ForumRpc.UserDetail(ctx, req)
	if err != nil {
		return nil, errors.New(forumUserSingleError)
	}

	return helper.ForumUserToGraphQL(res), nil
}

func (r *queryResolver) ForumUsers(
	ctx context.Context,
	first *int,
	last *int,
	after *string,
	before *string,
	sorter []*model.ForumUserConnectionSorter,
	filter *model.ForumUserConnectionFilter,
) (*model.ForumUserConnection, error) { ////
	userId := auth.ForumUserIDFromContext(ctx)

	req := &forumclient.UserListRequest{
		VisitorId: int64(userId),
	}

	preload, eagerLoad := helper.GetForumUserNodePreloads(ctx)
	if preload != "" {
		req.Preload = preload
	}
	if eagerLoad != "" {
		req.EagerLoad = eagerLoad
	}

	pagination := util.NewPagination(first, last, after, before,
		r.svcCtx.Config.Query.DefaultAmount, r.svcCtx.Config.Query.MaxAmount)

	sorterMap := helper.GetForumUserConnectionSorterMap(pagination, sorter)
	var reverse bool
	if sorterMap != nil {
		for k, v := range sorterMap {
			switch k {
			case "offset":
				if i, err := strconv.ParseInt(v, 10, 32); err == nil {
					req.Offset = int32(i)
				}
			case "limit":
				if i, err := strconv.ParseInt(v, 10, 32); err == nil {
					req.Limit = int32(i)
				}
			case "sorter":
				req.Sorter = v
			case "reverse":
				if v == "1" {
					reverse = true
				}
			}
		}
	}

	filterMap := helper.GetForumUserConnectionFilterMap(filter)
	if filterMap != nil {
		for k, v := range filterMap {
			switch k {
			case "search":
				req.Search = v
			case "filter":
				req.Filter = v
			case "preFilterEntity":
				req.PreFilterEntity = v
			case "preFilter":
				req.PreFilter = v
			}
		}
	}

	res, err := r.svcCtx.ForumRpc.UserList(ctx, req)
	if err != nil {
		return &model.ForumUserConnection{}, errors.New(forumUserListError)
	}

	if len(res.Data) > 0 {
		var users []*model.ForumUser
		for _, user := range res.Data {
			users = append(users, helper.ForumUserToGraphQL(user))
		}
		connection := helper.GetForumUserConnection(users,
			res.HasNextPage > 0, res.HasPreviousPage > 0, req.Offset, req.Limit, reverse)
		return connection, nil
	}

	return &model.ForumUserConnection{}, nil
}

func (r *queryResolver) ForumFriendship(ctx context.Context, id string) (*model.ForumFriendship, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) ForumFriendships(ctx context.Context, first *int, last *int, after *string, before *string, sorter []*model.ForumFriendshipConnectionSorter, filter *model.ForumFriendshipConnectionFilter) (*model.ForumFriendshipConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

// ForumUser returns generated.ForumUserResolver implementation.
func (r *Resolver) ForumUser() generated.ForumUserResolver { return &forumUserResolver{r} }

type forumUserResolver struct{ *Resolver }
